import { defineStore } from "pinia";
import { ref } from 'vue';
import { EquipmentScreen } from '@/api/screen/equipment';

export const UseScreenEqTree = defineStore('screenTree', () => {


    const videoDisConnect = ref(false);

    const setVideoDisConnect = (val: boolean) => {
        videoDisConnect.value = val
    }

    const data = ref([]);
    const getEquipementTree = async () => {
        let res: any = await EquipmentScreen('ALL', '监控设备');
        let tempArr = res.data
        // 一级处理
        tempArr.forEach((el, index) => {
            if (el.children) {
                tempArr[index].children = el.children.filter((item, i) => {

                    return !item.name.includes('走廊') && !item.name.includes('厕所') && !item.name.includes('灯光') && !item.name.includes('动环') && !item.name.includes('空调') && !item.name.includes('门禁')
                });

            }
        });
        // 二级处理
        tempArr.forEach((el, index) => {
            if (el.children) {
                el.children.forEach((c, i) => {
                    if (c.children) {
                        tempArr[index].children[i].children = tempArr[index].children[i].children.filter((j, k) => {

                            return !j.name.includes('温湿度') && !j.name.includes('路由') && !j.name.includes('烟雾') && !j.name.includes('漏水绳') && !j.name.includes('灯光') && !j.name.includes('动环') && !j.name.includes('空调') && !j.name.includes('门禁')
                        })

                    }
                });
            }
        });

        //递归处理

        const mapTree = (treeData) => {
            treeData.forEach(treeNode => {
                treeNode.checked = false
                if (treeNode?.children?.length) {
                    mapTree(treeNode.children)
                }
            })
        }

        mapTree(tempArr)
        function getLeafNodes(node: any): any[] {
            const leafNodes: any[] = [];
            console.log(node, 'treeData+++++++');
            function traverse(node: any) {

                if (node.type == '0' && node.children) {

                    // console.log(node, 'treeData+++++++');
                    node.children.forEach(child => {
                        traverse(child);
                    });
                } else if (node.type == '1') {
                    leafNodes.push(node);
                }
            }

            traverse(node);
            return leafNodes;
        }
        // let children = [];
        const getTreeChildren = (treeData) => {
            console.log(treeData, 'total+++');


            treeData.forEach(treeNode => {
                let a = getLeafNodes(treeNode)
                if (a.length > 0) {
                    treeNode.children = a
                } else {
                    treeNode.children = []
                }


            })


        }


        getTreeChildren(tempArr)


        data.value = tempArr;

    }

    return { data, videoDisConnect, setVideoDisConnect, getEquipementTree }
})